#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <sys/epoll.h>
#include "thpool.h"
#include "hlog.h"
int lfd;
int epfd;
void mylogger(int loglevel, const char* buf, int len) {
    if (loglevel >= LOG_LEVEL_ERROR) {
        stderr_logger(loglevel, buf, len);
    }

    if (loglevel >= LOG_LEVEL_INFO) {
        file_logger(loglevel, buf, len);
    }

    // network_logger(loglevel, buf, len);
}
void do_work(void *epoll_fd)
{
    // 等待接收连接请求
    struct sockaddr_in client;
    int len = sizeof(client);
    // all = (struct epoll_event *) all;
    int fd = (int)epoll_fd;
    if (fd == lfd)
    {
        // 接收新连接请求
        int cfd = accept(fd, (struct sockaddr *)&client, &len);
        if (cfd == -1)
        {
            hloge("客户端链接失败\n");
            exit(1);
        }
        // 打印客户端信息
        char ip[64];
        hlogi("client IP: %s, port: %d\n", inet_ntop(AF_INET, &client.sin_addr.s_addr, ip, sizeof(ip)), ntohs(client.sin_port));

        struct epoll_event temp;
        temp.data.fd = cfd;
        temp.events = EPOLLIN;
        // 将新连接的客户端挂到epoll树上
        epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &temp);
    }
    else
    {
        char buf[1024];
        // 客户端发送过来一些数据
        int len = recv(fd, buf, sizeof(buf), 0);
        if (len == -1)
        {
            hloge("read error\n");
            exit(1);
        }
        else if (len == 0)
        {
            hlogi("客户端断开了链接\n");
            // 从epoll树上删除
            epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
            close(fd);
        }
        else
        {
            hlogi("revc %s\n", buf);
            write(fd, buf, len);
        }
    }
}
int main(int argc, const char *argv[])
{
    // htimer_add(loop, timer_write_log, 1000, INFINITE);
    // define hlog default_logger()
    logger_set_handler(hlog, mylogger);
    // 设置log的文件名
    hlog_set_file("loop.log");
    // 设置log开启颜色
    logger_enable_color(hlog, 1);
    // 创建一个logger
    hlog_set_file("epoll.log");


    // 创建套接字
    lfd = socket(AF_INET, SOCK_STREAM, 0);
    // 绑定本地IP和端口
    struct sockaddr_in serv;
    memset(&serv, 0, sizeof(serv));
    serv.sin_port = htons(9999);
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    serv.sin_family = AF_INET;
    bind(lfd, (struct sockaddr *)&serv, sizeof(serv));

    // 监听
    listen(lfd, 128);

    
    // 创建epoll树的根节点
    epfd = epoll_create(2000);
    struct epoll_event ev;
    ev.events = EPOLLIN;
    ev.data.fd = lfd;
    // 将监听套接字加到epoll 树上
    epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &ev);

    struct epoll_event all[2000];

    threadpool thpool = thpool_init(20);
    while (1)
    {
        // 委托内核进行操作
        int ret = epoll_wait(epfd, all, sizeof(all) / sizeof(all[0]), -1);
        // 如果有新客户端链接
        for (int i = 0; i < ret; ++i)
        {
            thpool_add_work(thpool, do_work, (void *)all[i].data.fd);
        }
        // 不知道为什么没有添加这个 wait 会出错
        thpool_wait(thpool);

        // 客户端有信息发送过来
    }

    close(lfd);

    return 0;
}
